package com.youlian.activity;


import android.app.TabActivity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageButton;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.RadioGroup.OnCheckedChangeListener;
import android.widget.RelativeLayout;
import android.widget.TabHost;

import com.youlian.common.Configure;
import com.youlian.utils.MyLog;
import com.youlian.utils.Utils;
import com.youlian.utils.connection.BaseExecutor;
import com.youlian.utils.connection.BaseTask;
import com.youlian.utils.connection.OnTaskHandlerListener;
import com.youlian.view.SearchView;

public class YoulianActivity extends TabActivity implements OnClickListener,OnTaskHandlerListener,OnCheckedChangeListener {
    /** Called when the activity is first created. */
	public static String TAB_TAG_HOME = "home";
	public static String TAB_TAG_CHANNEL = "channel";
	public static String TAB_TAG_ACCOUNT = "account";
	public static String TAB_TAG_SEARCH = "search";
	public static String TAB_TAB_MORE = "more";

	protected BaseExecutor mConnectionTask;
	public static TabHost mTabHost;
	// Animation
	private Animation left_in, left_out;
	private Animation right_in, right_out;

//	private RelativeLayout title;
    private RadioGroup radioGroup;
    private RadioButton radioHome,radioSearch,radioKabao,radioAdvert,radioMore;
//    private ImageButton cateBtn;
//    private Button loaclBtn;
    private Intent mHomeItent, mSearchIntent, mKabaoIntent, mAdvertIntent,mMoreIntent;
    
    private int groupCount;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);
		mConnectionTask = new BaseExecutor(this);
		Configure.init(this);
        initView();
        initListener();
		prepareAnim();
    }

    private void initView(){
    	radioGroup = (RadioGroup)findViewById(R.id.main_radiogroup);
    	groupCount = radioGroup.getChildCount();
    	RadioButton button = (RadioButton) radioGroup.getChildAt(0);
    	button.setEnabled(true);
    	button.setChecked(true);
//    	title = (RelativeLayout)findViewById(R.id.main_title);
//    	cateBtn =  (ImageButton)findViewById(R.id.btn_cate);
//    	loaclBtn =  (Button)findViewById(R.id.btn_reght);

		mHomeItent = new Intent(this, HomeActivity.class);
		mSearchIntent = new Intent(this, SearchActivity.class);
		mKabaoIntent = new Intent(this, KabaoActivity.class);
		mAdvertIntent = new Intent(this, ActionActivity.class);
		mMoreIntent = new Intent(this, MoreActivity.class);
		mTabHost = getTabHost();
		mTabHost.addTab(buildTabSpec(TAB_TAG_HOME, R.string.category_home, R.drawable.logo, mHomeItent));
		mTabHost.addTab(buildTabSpec(TAB_TAG_CHANNEL,R.string.category_channel, R.drawable.logo, mSearchIntent));
		mTabHost.addTab(buildTabSpec(TAB_TAG_SEARCH, R.string.category_search,R.drawable.logo, mKabaoIntent));
		mTabHost.addTab(buildTabSpec(TAB_TAG_ACCOUNT,R.string.category_account, R.drawable.logo, mAdvertIntent));
		mTabHost.addTab(buildTabSpec(TAB_TAB_MORE, R.string.category_more,R.drawable.logo, mMoreIntent));
		mTabHost.setCurrentTabByTag(TAB_TAG_HOME);
    }

    private void initListener(){
    	radioGroup.setOnCheckedChangeListener(this);
//    	cateBtn.setOnClickListener(this);
//    	loaclBtn.setOnClickListener(this);
    }
    private void prepareAnim() {
		left_in = AnimationUtils.loadAnimation(this, R.anim.left_in);
		left_out = AnimationUtils.loadAnimation(this, R.anim.left_out);

		right_in = AnimationUtils.loadAnimation(this, R.anim.right_in);
		right_out = AnimationUtils.loadAnimation(this, R.anim.right_out);
	}


    private TabHost.TabSpec buildTabSpec(String tag, int resLabel, int resIcon,final Intent content) {
		return mTabHost.newTabSpec(tag).setIndicator(getString(resLabel),getResources().getDrawable(resIcon)).setContent(content);
	}

	public static void setCurrentTabByTag(String tab) {
		mTabHost.setCurrentTabByTag(tab);
	}


	@Override
	public void onClick(View v) {
		switch (v.getId()) {
		case R.id.btn_cate:
			Intent mCategoryActivity = new Intent(this, CategoryActivity.class);
			startActivity(mCategoryActivity);
			break;
		case R.id.btn_account:
			break;
		case R.id.btn_reght:
			Intent mChangeCityActivity = new Intent(this, ChangeCityActivity.class);
			startActivity(mChangeCityActivity);
			break;
		default:
			break;
		}

	}


	private void sync(BaseTask task){
		if(!Utils.notNull(mConnectionTask)){
			MyLog.error(getClass(), "---mConnectionTask null------");
			mConnectionTask = new BaseExecutor(this);
		}
		mConnectionTask.connectInQueue(task);
	}
	/**
	 * stop all the thread
	 */
	protected void stopTask(){
		if(Utils.notNull(mConnectionTask)){
			mConnectionTask.disconnect();
			MyLog.debug(getClass(), "------mConnectionTask close----------");
		}
	}

	@Override
	public Object onConnection(int action, Object... params) throws Exception {
		// TODO Auto-generated method stub
		return action;
	}

	@Override
	public void onProcessData(int action, Object data, Object... params) {
		// TODO Auto-generated method stub

	}

	@Override
	public void onException(int action, Exception exception, Object... params) {
		// TODO Auto-generated method stub

	}

	@Override
	protected void onDestroy() {
		// TODO Auto-generated method stub
		super.onDestroy();
		MyLog.debug(getClass(), "------------------stopTask-------------------");
		stopTask();
		mConnectionTask = null;
	}

	int mCurTabId = R.id.radio_home;

	@Override
	public void onCheckedChanged(RadioGroup group, int checkedId) {
		// TODO Auto-generated method stub
		MyLog.debug(getClass(), "checkedId:"+checkedId);
		if (mCurTabId ==checkedId) {
			return;
		}
		 boolean o = true;
			if (mCurTabId < checkedId)
				o = true;
			else
				o = false;
			if (o)
				mTabHost.getCurrentView().startAnimation(left_out);
			else
				mTabHost.getCurrentView().startAnimation(right_out);
		switch (checkedId) {
		case R.id.radio_home:
			mTabHost.setCurrentTabByTag(TAB_TAG_HOME);
			break;
		case R.id.radio_search:
			new SearchView(this,Configure.screenWidth,(int)((int)Configure.screenHeight*0.8));
			break;
		case R.id.radio_kabao:
			mTabHost.setCurrentTabByTag(TAB_TAG_SEARCH);

			break;
		case R.id.radio_advert:
			mTabHost.setCurrentTabByTag(TAB_TAG_ACCOUNT);

			break;
		case R.id.radio_more:
			mTabHost.setCurrentTabByTag(TAB_TAB_MORE);
			break;

		default:
			break;
		}
			if (o)
				mTabHost.getCurrentView().startAnimation(left_in);
			else
				mTabHost.getCurrentView().startAnimation(right_in);
			mCurTabId = checkedId;
	}
}